package com.example.escapechardemo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.LogUtils
import com.example.escapechardemo.bean.AttributesBean
import com.example.escapechardemo.utils.JsonUtils.Companion.getReplace
import com.google.gson.Gson
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 *@author: njb
 *@date:  2023/4/16 13:38
 *@desc:
 */
class MainActivity :AppCompatActivity(){
    private val TAG :String = "MainLog"
    private val jsonObject :JSONObject by lazy {
        JSONObject()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initData();
    }

    private fun initData() {
        val acAttributesBean = AttributesBean()
        try {
            acAttributesBean.let {
                it.status = "on"
                it.temperature = "22"
                it.fanSpeed = "3"
                it.mode = "low"
            }
            val attributes = Gson().toJson(acAttributesBean)
            LogUtils.d(TAG, "--构建的设备信息为--$attributes")
            jsonObject.also {
                it.put("address","01")
                it.put("deviceType","03")
                it.put("friendlyName","客厅空调")
                it.put("attributes", attributes)
                LogUtils.d(TAG, "--转义前的设备信息为--$it")
                val list: MutableList<String> = ArrayList()
                list.add(getReplace(it.toString()))
                LogUtils.d(TAG, "--转义后的设备上报数组为--$list")
                it.put("devices", list)
                LogUtils.d(TAG, "--设备上报信息为--$list")
                val jsonArray = JSONArray()
                jsonArray.put(it)
                LogUtils.d(TAG, "--转义前的设备上报信息json数组为--$it")
                LogUtils.d(TAG, "--转义后的设备上报信息json数组为--${getReplace(it.toString())}")
            }
        }catch (e: JSONException){
            e.printStackTrace()
        }
    }
}